package com.roadjava.GDOUGym.controller.admin;


import com.roadjava.GDOUGym.bean.dto.CompetitionDTO;
import com.roadjava.GDOUGym.bean.entity.CompetitionTeamDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.bean.res.TableResult;
import com.roadjava.GDOUGym.service.CompetitionService;
import com.roadjava.GDOUGym.service.CompetitionTeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/competitionTeam")
public class CompetitionTeamController {
    @Resource
    private CompetitionTeamService competitionTeamService;

    //添加比赛
    @RequestMapping("/add.do") @ResponseBody
    public ResultDTO<String> add(@RequestBody CompetitionTeamDO dto) {
        try {
            return competitionTeamService.add(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("添加场地异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("添加场地异常");
        }
    }



    //删除场地
    @RequestMapping("/delete.do") @ResponseBody
    public ResultDTO<String> delete(@RequestBody CompetitionTeamDO dto) {
        try {
            return competitionTeamService.delete(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("删除场地异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("删除场地异常");
        }
    }

    //修改场地
    @RequestMapping("/change.do") @ResponseBody
    public ResultDTO<String> change(@RequestBody CompetitionTeamDO dto) {
        try {
            return competitionTeamService.change(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("修改场地异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("修改场地异常");
        }
    }

    //查询场地
    @RequestMapping ("find.do") @ResponseBody
    public ResultDTO<TableResult<CompetitionTeamDO>> find(@RequestBody CompetitionTeamDO dto){
        try {
            TableResult<CompetitionTeamDO> tableResult = new TableResult<>();
            // service层返回
            ResultDTO<List<CompetitionTeamDO>>  competitionDTO = competitionTeamService.find(dto);
            tableResult.setRows(competitionDTO.getData());
            tableResult.setTotalCount(competitionDTO.getTotal());
            return ResultDTO.buildSuccess(tableResult);
        }catch (Exception e) {
            log.error("查询赛事异常",e);
            throw new RuntimeException("查询赛事异常");
        }
    }


}
